package main.java.com.example.mainstream.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.shopapp.bean.ShopAddress;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件名：ShopAddressDBUtils
 * 描述：
 */
public class ShopAddressDBUtils {

    public static final String DB_NAME = "shop_address_dbname";

    public static final int VERSION = 1;

    private static ShopAddressDBUtils sqliteDB;

    private SQLiteDatabase db;

    private ShopAddressDBUtils(Context context) {
        ShopAddressHelper dbHelper = new ShopAddressHelper(context, DB_NAME, null, VERSION);
        db = dbHelper.getWritableDatabase();
    }

    /**
     * ��ȡSqliteDBʵ��
     * @param context
     */
    public synchronized static ShopAddressDBUtils getInstance(Context context) {
        if (sqliteDB == null) {
            sqliteDB = new ShopAddressDBUtils(context);
        }
        return sqliteDB;
    }



    public void delete(Context context,String id) {
        ShopAddressHelper dbHelper = new ShopAddressHelper(context, DB_NAME, null, VERSION);
        db = dbHelper.getReadableDatabase();
        db.delete("ShopAddress", "id=?", new String[] { id });
    }

    /**
     * 修改信息
     * @param context
     * @param shopAddress
     */
    public void change(Context context, ShopAddress shopAddress) {
        ShopAddressHelper dbHelper = new ShopAddressHelper(context, DB_NAME, null, VERSION);
        db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("id", shopAddress.getId());
        values.put("user_id", shopAddress.getUser_id());
        values.put("address", shopAddress.getAddress());
        values.put("address_id", shopAddress.getAddress_id());
        values.put("mobile", shopAddress.getMobile());
        values.put("city", shopAddress.getCity());
        values.put("defaultcity", shopAddress.getDefaultcity());
        values.put("user", shopAddress.getUser());
        db.update("ShopAddress", values, "id=?", new String[]{shopAddress.getId()+""});
    }

    /**
     *
     * @param shopAddress
     * @return
     */
    public int   insert(ShopAddress  shopAddress) {
        try {
            db.execSQL("insert into ShopAddress(user_id,user,address_id,mobile,city,defaultcity,address) values(?,?,?,?,?,?,?) ", new String[]{
                    shopAddress.getUser_id(),
                    shopAddress.getUser(),
                    shopAddress.getAddress_id(),
                    shopAddress.getMobile(),
                    shopAddress.getCity(),
                    shopAddress.getDefaultcity(),
                    shopAddress.getAddress()
            });
            return 1;
        } catch (Exception e) {
            Log.d("����", e.getMessage().toString());
            return 0;
        }
    }

    /**
     * 获取收获地址信息
     * @param user_id
     * @return
     */
    @SuppressLint("Range")
    public List<ShopAddress>  getListShopAddress(String user_id){
        List<ShopAddress> shopAddressList = new ArrayList<>();
        Cursor cursor = db
                .query("ShopAddress", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do{
                if(user_id.equals(cursor.getString(cursor.getColumnIndex("user_id")))){
                    ShopAddress shopAddress = new ShopAddress();
                    shopAddress.setId(cursor.getInt(cursor.getColumnIndex("id")));
                    shopAddress.setUser_id(cursor.getString(cursor.getColumnIndex("user_id")));
                    shopAddress.setUser(cursor.getString(cursor.getColumnIndex("user")));
                    shopAddress.setAddress(cursor.getString(cursor.getColumnIndex("address")));
                    shopAddress.setAddress_id(cursor.getString(cursor.getColumnIndex("address_id")));
                    shopAddress.setMobile(cursor.getString(cursor.getColumnIndex("mobile")));
                    shopAddress.setCity(cursor.getString(cursor.getColumnIndex("city")));
                    shopAddress.setDefaultcity(cursor.getString(cursor.getColumnIndex("defaultcity")));
                    shopAddressList.add(shopAddress);
                }
            }while (cursor.moveToNext());
        }
        return shopAddressList;
    }

    /**
     * 获取收获地址信息
     * @param user_id
     * @return
     */
    @SuppressLint("Range")
    public ShopAddress getShopAddress(String user_id){
        ShopAddress shopAddress = null;
        Cursor cursor = db
                .query("ShopAddress", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do{
                if(user_id.equals(cursor.getString(cursor.getColumnIndex("address_id")))){
                     shopAddress = new ShopAddress();
                    shopAddress.setId(cursor.getInt(cursor.getColumnIndex("id")));
                    shopAddress.setUser_id(cursor.getString(cursor.getColumnIndex("user_id")));
                    shopAddress.setUser(cursor.getString(cursor.getColumnIndex("user")));
                    shopAddress.setAddress(cursor.getString(cursor.getColumnIndex("address")));
                    shopAddress.setAddress_id(cursor.getString(cursor.getColumnIndex("address_id")));
                    shopAddress.setMobile(cursor.getString(cursor.getColumnIndex("mobile")));
                    shopAddress.setCity(cursor.getString(cursor.getColumnIndex("city")));
                    shopAddress.setDefaultcity(cursor.getString(cursor.getColumnIndex("defaultcity")));
                    return shopAddress;
                }
            }while (cursor.moveToNext());
        }
        return shopAddress;
    }
}
